package com.antler.luajavalibrary.luaext.utils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.util.TypeUtils;

public class FastJSONUtils {
    public static String getString(JSONObject object, String key, String def) {
        String result = def;
        do {
            if (object == null) {
                break;
            }

            Object value = object.get(key);
            if (value == null) {
                break;
            }

            result = value.toString();
        } while (false);
        return result;
    }

    public static Boolean getBoolean(JSONObject object, String key, Boolean def) {
        Boolean result = def;
        do {
            if (object == null) {
                break;
            }

            Object value = object.get(key);
            if (value == null) {
                break;
            }

            try {
                result = TypeUtils.castToBoolean(value);
                if (result == null) {
                    result = def;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        } while (false);
        return result;
    }

    public static Integer getInteger(JSONObject object, String key, Integer def) {
        Integer result = def;
        do {
            if (object == null) {
                break;
            }

            Object value = object.get(key);
            if (value == null) {
                break;
            }

            try {
                result = TypeUtils.castToInt(value);
                if (result == null) {
                    result = def;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        } while (false);
        return result;
    }

    public static int getIntValue(JSONObject object, String key, int def) {
        int result = def;
        do {
            Integer value = getInteger(object, key, Integer.valueOf(def));
            result = value.intValue();
        } while (false);
        return result;
    }

    public static Float getFloat(JSONObject object, String key, Float def) {
        Float result = def;
        do {
            if (object == null) {
                break;
            }

            Object value = object.get(key);
            if (value == null) {
                break;
            }

            try {
                result = TypeUtils.castToFloat(value);
                if (result == null) {
                    result = def;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        } while (false);
        return result;
    }

    public static float getFloatValue(JSONObject object, String key, float def) {
        float result = def;
        do {
            Float value = getFloat(object, key, Float.valueOf(def));
            result = value.floatValue();
        } while (false);
        return result;
    }
}
